﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NFlickr.Core;
using System.Collections;
using NFlickr.Core.Search;
using WindowsFormsApplication1;

namespace NFlickr.Win
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        NFlickr.Core.FlickrService service = new FlickrService();
        private void cmdLogin_Click(object sender, EventArgs e)
        {
            service.Permission = NFlickr.Resource.Permission.Write;
            Frob frob = service.Frob;
            string frobValue = frob.Value;

            FlickrUrl filckrUrl = service.FlickrUrl;

            string loginUrl = filckrUrl.Url;

            System.Diagnostics.Process.Start(loginUrl);
        }

        private void cmdUpload_Click(object sender, EventArgs e)
        {
            Token token = service.Token;
            string tokenValue = token.Value;

            //Photo photo = service.CreatePhoto(
            //    false,
            //    NFlickr.Resource.SafetyLevel.None,
            //    true,
            //    true,
            //    true,
            //    new List<string> { "Test", "NFlickr" },
            //    "Test",
            //    "This is uploaded from the NFlickr");
            IList<PhotoSet> list = service.PhotoSetList;

            string photoSetName = "Test";

            Photo photo = new Photo(service)
            {
                Hidden = false,
                SafetyLevel = NFlickr.Resource.SafetyLevel.None,
                Public = true,
                Family = true,
                Friend = true,
                Tags = new List<string> { "Test", "NFlickr" },
                Title = photoSetName,
                Description = "This is uploaded from the NFlickr"
            };

            var setFilter = from filterSet in list
                            where filterSet.Title == photoSetName
                            select filterSet;
            photo = service.Upload(photo, txtFileName.Text);


            if (setFilter.Count() == 0)
            {
                PhotoSet photoSet = new PhotoSet(service) { Title = photoSetName, Description = "The first PhotoSet" };
                photoSet = service.CreatePhotoSet(photoSet, photo);
            }
            else
            {
                PhotoSet photoSet = setFilter.First();
                photoSet.Add(photo);

            }






        }

        private void cmdBrowser_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dlg = new OpenFileDialog())
            {
                dlg.Filter = "Image Files|*.jpg";
                DialogResult result = dlg.ShowDialog();
                if (result == DialogResult.OK)
                {
                    txtFileName.Text = dlg.FileName;
                }
            }
        }

        private void cmdPhotoSets_Click(object sender, EventArgs e)
        {
            IList<PhotoSet> list = service.PhotoSetList;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Token token = service.Token;
            string tokenValue = token.Value;
            //PhotoSearch photoSearch = new PhotoSearch
            //{
            //    PhotoTags = new List<string> { "Yang" },
            //    SearchPrivacy = NFlickr.Resource.SearchPrivacy.ByFriend,
            //    TagMode = NFlickr.Resource.TagMode.AND,
            //    User = service.Token.User
            //};
            //service.Search(photoSearch);

            PhotoList list = new PhotoList(service);
            var photos = from photo in list
                              where photo.TagName == "Yang" && photo.Family == true 
                              select photo;
        }
    }
}
